package com.wpam.scrabble.mobile.messages;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class MsgSerializer {
	
	public static byte[] serialize(Message msg) {

	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    try {
	    	ObjectOutputStream os = new ObjectOutputStream(out);
			os.writeObject(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return out.toByteArray();
	}

	public static Message deserialize(byte[] bytes) {

	    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
	    try {
		    ObjectInputStream is = new ObjectInputStream(in);
		    return (Message)is.readObject();
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
	    } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return null;
	}
}
